package hiatao.puzzle.adapter;

import haitao.puzzle.R;
import hiatao.puzzle.entity.PuzzleInfo;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.PixelUtil;
import hiatao.puzzle.utils.Tools;
import java.util.ArrayList;
import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class StaggeredAdapter  extends BaseAdapter {

    private Context mContext;
    private ArrayList<PuzzleInfo> mInfos;

    public StaggeredAdapter(Context context) {
        mContext = context;
        mInfos = new ArrayList<PuzzleInfo>();
    }

    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        PuzzleInfo duitangInfo = mInfos.get(position);

        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
            convertView = layoutInflator.inflate(R.layout.list_infos, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.news_pic);
            holder.head_img = (ImageView) convertView.findViewById(R.id.head_img);
            holder.like_tv = (TextView) convertView.findViewById(R.id.like_tv);
            holder.intro_tv = (TextView) convertView.findViewById(R.id.intro_tv);
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            convertView.setTag(holder);
        }else {
        	holder = (ViewHolder) convertView.getTag();
        }

        Tools.log(duitangInfo.getPicture());
		LayoutParams lp = (LayoutParams) holder.imageView.getLayoutParams();
		final int s= PixelUtil.getScreenWidth(mContext);
/*		lp.width = s/2;
		if(position==0){
			// 先 除以2 图片 原图不会变形   
			lp.width = (int) (s/2);
			lp.height=(int) (s/1.4);
			//holder.imageView.setScaleType(ScaleType.CENTER_CROP);
			final ImageView img= holder.imageView;
			ImageLoader.getInstance().displayImage(duitangInfo.getPicture(), holder.imageView, ImageUtils.pictureOptions(), new SimpleImageLoadingListener(){

				public void onLoadingComplete(String imageUri, android.view.View view, final android.graphics.Bitmap loadedImage) {
					//img.setScaleType(ScaleType.FIT_XY);
					img.getLayoutParams().width=(int) (s/1.4);;
            	};
			});
		}else{
			lp.height=s/2;
			holder.imageView.setLayoutParams(lp);
			//holder.imageView.setScaleType(ScaleType.FIT_XY);
			ImageUtils.loadImg(duitangInfo.getPicture(), holder.imageView);
		}*/
		lp.width = (int) (s);
		lp.height=(int) (s);
		holder.imageView.setScaleType(ScaleType.FIT_XY);
		ImageUtils.loadImg(duitangInfo.getPicture(), holder.imageView);
		
		holder.like_tv.setText(String.valueOf(duitangInfo.getLikeNum()));
		holder.intro_tv.setText(duitangInfo.getIntro());
		holder.name_tv.setText(duitangInfo.getName());
		ImageUtils.loadHeadImg(duitangInfo.getUserHead(), holder.head_img);
        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        ImageView head_img;
        TextView like_tv;
        TextView name_tv;
        TextView intro_tv;
    }

    public void RefeshClear(List<PuzzleInfo> arrayList) {
    	mInfos.clear();
    	mInfos.addAll(arrayList);
    	notifyDataSetChanged();
    }
    
    public void clear() {
    	mInfos.clear();
    }
    
    @Override
    public int getCount() {
    	return mInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public void addItemLast(List<PuzzleInfo> datas) {
        mInfos.addAll(datas);
        notifyDataSetChanged();
    }
}
